﻿@using EcBlog.Admin.Models
@using EcBlog.Properties
@using Telerik.Web.Mvc.UI
@using System.Collections
@{  
    ViewBag.Title = "Manage Pages";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
@section head {
    @Html.Partial("_Head")
    <script type="text/javascript">
        function onError(e) {
            e.preventDefault();
            alert(e.XMLHttpRequest.getResponseHeader("message"));
        }

        function onLoadPageGrid() {
            replaceNewButtonText($(this), '@ModelLocalization.NewPage');
        }
        function onDataBoundPageGrid() {
            replaceDeleteConfirmation(this, '@ModelLocalization.DeleteConfirmation', '@ModelLocalization.Page');
        }
        function onEditPageGrid(e) {
            replaceEditPopupTitle(e, '@ModelLocalization.NewPage', '@ModelLocalization.EditPage');
            hideEditPopupField(e, 'PageId');
            var published = $('#Published').data("tDateTimePicker");
            published.value(e.dataItem.Published);

            $(e.form).closest(".t-window-content").width(1024).height(700);
        }

        function onLoadCommentGrid() {
            replaceNewButtonText($(this), '@ModelLocalization.NewComment');
        }
        function onDataBoundCommentGrid() {
            replaceDeleteConfirmation(this, '@ModelLocalization.DeleteConfirmation', '@ModelLocalization.Comment');
        }
        function onEditCommentGrid(e) {
            replaceEditPopupTitle(e, '@ModelLocalization.NewComment', '@ModelLocalization.EditComment');
            hideEditPopupField(e, 'CommentId'); 
        }

        function onLoadTagGrid() {
            replaceNewButtonText($(this), '@ModelLocalization.NewTag');
        }
        function onDataBoundTagGrid() {
            replaceDeleteConfirmation(this, '@ModelLocalization.DeleteConfirmation', '@ModelLocalization.Tag');
        }
        function onEditTagGrid(e) {
            replaceEditPopupTitle(e, '@ModelLocalization.NewTag', '@ModelLocalization.EditTag');
            hideEditPopupField(e, 'PageId');
        }
    </script>
    <style>
        
        
        #ContentHtml
        {
            height: 650px;
            width: 800px;
        }
    </style>
}
@(Html.Telerik().Grid<EcBlog.Models.Page>()
      .Name("PageGrid")
      .Columns(columns =>
          {
              columns.Command(commands => commands.Delete()
                                              .ButtonType(GridButtonType.Image)).Width(80);
              columns.AutoGenerate(c => { });
          })
      .ClientRowTemplate(grid =>
                         "<div class='page-details'>" +
                         "<h3 class='t-widget'><#= Title #></h3>" +
                         "<dl>" +
                         "<dt>Slug:</dt><dd><#= Slug #></dd>" +
                         "<dt>LayoutId:</dt><dd><#= LayoutId #></dd>" +
                         "</dl><dl>" +
                         "<dt>IsCommentable:</dt><dd><#= IsCommentable #></dd>" +
                         "<dt>Created:</dt><dd><#= Created #></dd>" +
                         "<dt>LastModified:</dt><dd><#= LastModified #></dd>" +
                         "</dl><dl class='t-widget'>" +
                         "<dt>Published:</dt><dd><#= Published #></dd>" +
                         "<dt>IsPublish:</dt><dd><#= IsPublish #></dd>" +
                         "<dt></dt><dd><a href='../Admin/Page/Preview?pageId=<#= PageId #>' target='_blank'>Preview</a></dd>" +
                         "</dl>" +
                         "<div class='commands'>" + 
                         "<a class='t-button t-button-icon' href='/Admin/Page/NewPage?id=<#= PageId #>'><span class='t-icon t-edit'></span></a>"
                         + grid.DeleteButton(null, GridButtonType.Image) + "</div>" +
                         "</div>")
      .DataBinding(dataBinding =>
                   dataBinding.Ajax()
                       .Select("PageList", "Page", new { area = "Admin" })
                       .Delete("Delete", "Page", new { area = "Admin" })
                       .Update("Update", "Page", new { area = "Admin" })
                       .Insert("Insert", "Page", new { area = "Admin" }))
      //.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.ImageAndText))
      .ToolBar(commands => commands.Custom().Text(ModelLocalization.NewPage).Action("NewPage","Page",new { area = "Admin" }))
      .DataKeys(keys => keys.Add(c => c.PageId).RouteKey("PageId"))
      .Pageable(p => p.PageSize(100))
      .Scrollable()
      .KeyboardNavigation()
      .Filterable()
      .Groupable()
      .Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn))
      .Reorderable(c => c.Columns(true))
      .Resizable(c => c.Columns(true))
      .Editable(c => c.Mode(GridEditMode.PopUp))
      .EnableCustomBinding(true)
      .ColumnContextMenu()
      .ClientEvents(events =>
          events
            .OnError("onError")
            .OnLoad("onLoadPageGrid")
            .OnDataBound("onDataBoundPageGrid")
            .OnEdit("onEditPageGrid"))
      .DetailView(d => d.ClientTemplate(
          Html.Telerik().TabStrip()
            .Name("TabStrip_<#= PageId #>")
            .SelectedIndex(0)
            .Items(items =>
            {
                items.Add().Text("Comments").Content(
                    Html.Telerik().Grid<EcBlog.Models.Comment>()
                        .Name("Comments_<#= PageId #>")
                        .RowAction(row =>
                        {
                            if (row.DataItem.Created == row.DataItem.LastModified)
                            {
                                row.HtmlAttributes["style"] = "font-weight: bold; color: red";
                            }
                        })
                        .Columns(columns =>
                        {
                            columns.Command(commands =>
                            {
                                commands.Edit()
                                    .ButtonType(GridButtonType.Image);
                                commands.Delete()
                                    .ButtonType(GridButtonType.Image);
                            }).Width(80);
                            columns.Bound(c => c.UserName);
                            columns.Bound(c => c.Email);
                            columns.Bound(c => c.Web);
                            columns.Bound(c => c.SendResponseMail);
                            columns.Bound(c => c.CommentHtml);
                            columns.Bound(c => c.IsPublished); 
                        })
                        .DataBinding(dataBinding => dataBinding.Ajax()
                            .Select("GetPageComments", "Page", new { pageId = "<#= PageId #>", area = "Admin" })
                            .Insert("InsertPageComment", "Page", new { pageId = "<#= PageId #>", area = "Admin" })
                            .Delete("DeletePageComment", "Page", new { pageId = "<#= PageId #>", area = "Admin" })
                            .Update("UpdatePageComment", "Page", new { pageId = "<#= PageId #>", area = "Admin" }))
                         .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.ImageAndText))
                         .DataKeys(keys => keys.Add(c => c.CommentId).RouteKey("CommentId"))
                         .Pageable(p => p.PageSize(100))
                         .Scrollable()
                         .KeyboardNavigation()
                         .Filterable()
                         .Groupable()
                         .Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn))
                         .Reorderable(c => c.Columns(true))
                         .Resizable(c => c.Columns(true))
                         .Editable(c => c.Mode(GridEditMode.PopUp))
                         .EnableCustomBinding(true)
                         .ColumnContextMenu()
                         .ClientEvents(events =>
                              events
                                .OnError("onError")
                                .OnLoad("onLoadCommentGrid")
                                .OnDataBound("onDataBoundCommentGrid")
                                .OnEdit("onEditCommentGrid"))
                        .ToHtmlString());

                items.Add().Text("Tags").Content(
                    Html.Telerik().Grid<PageTag>()
                        .Name("Tags_<#= PageId #>")
                        .Columns(columns =>
                        {
                            columns.Command(commands => commands.Delete().ButtonType(GridButtonType.Image)).Width(200);
                            columns.ForeignKey(c => c.TagId, (IEnumerable)ViewData["TagList"], "Id", "Name");
                        })
                        .DataBinding(dataBinding => dataBinding.Ajax()
                            .Select("GetPageTags", "Page", new { pageId = "<#= PageId #>", area = "Admin" })
                            .Insert("InsertPageTag", "Page", new { pageId = "<#= PageId #>", area = "Admin" })
                            .Delete("DeletePageTag", "Page", new { pageId = "<#= PageId #>", area = "Admin" }))
                        .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.ImageAndText))
                        .DataKeys(keys => keys.Add(c => c.TagId).RouteKey("TagId"))
                        .Pageable(p => p.PageSize(100))
                        .Scrollable()
                        .KeyboardNavigation()
                        .Filterable()
                        .Groupable()
                        .Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn))
                        .Reorderable(c => c.Columns(true))
                        .Resizable(c => c.Columns(true))
                        .Editable(c => c.Mode(GridEditMode.PopUp))
                        .EnableCustomBinding(true)
                        .ColumnContextMenu()
                        .ClientEvents(events =>
                          events
                            .OnError("onError")
                            .OnLoad("onLoadTagGrid")
                            .OnDataBound("onDataBoundTagGrid")
                            .OnEdit("onEditTagGrid"))
                            .ToHtmlString());

            }).ToHtmlString()
    ))
)
